<template>
	<view>
		<view>
			<view class="header_fixed">
				<scroll-view v-if="parkingSpaces.length <5 && parkingSpaces.length >1" class="bg-white nav">
					<view class="flex text-center">
						<view class="cu-item flex-sub" :class="item.psId==curParkingSpace.psId?'text-green cur':''" v-for="(item,index) in parkingSpaces"
						 :key="index" @tap="switchParkingSpace(item)" :data-id="index">
							{{item.num}}车位
						</view>
					</view>
				</scroll-view>
				<scroll-view v-if="parkingSpaces.length >4" scroll-x class="bg-white nav" scroll-with-animation scroll-left="true">
					<view class="cu-item flex-sub" :class="item.psId==curParkingSpace.psId?'text-green cur':''" v-for="(item,index) in parkingSpaces"
					 :key="index" @tap="switchParkingSpace(item)" :data-id="index">
						{{item.num}}车位
					</view>
				</scroll-view>
			</view>
			<view v-if="parkingSpaces.length == 1" class="block__title">{{parkingSpaces[0].num}}车位</view>
			<view v-if="parkingSpaces.length > 1" class="margin-header-top"></view>
			<view v-if="noData == false" >
				<view v-for="(item,index) in moreParkingSpaces" :key="index" class="bg-white margin-bottom margin-right-xs radius margin-left-xs padding-top padding-left padding-right">
					<view class="flex padding-bottom-xs solid-bottom justify-between">
						<view>{{item.feeName}}</view>
						<view class="text-gray">{{item.feeStateName}}</view>
					</view>
					<view class="flex margin-top justify-between">
						<view class="text-gray">费用编码</view>
						<view class="text-gray">{{item.feeId}}</view>
					</view>
					<view class="flex margin-top justify-between">
						<view class="text-gray">车牌号码</view>
						<view class="text-gray">{{curParkingSpace.carNum}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">状态</view>
						<view class="text-gray">{{item.stateName}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">每月金额</view>
						<view class="text-gray">{{item.feePrice}}元</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">到期时间</view>
						<view class="text-gray">{{item.endTime}}</view>
					</view>
					<view class="solid-top flex justify-end margin-top padding-top-sm padding-bottom-sm">
						<button class="cu-btn sm line-gray" @click="payFeeDetail(item)">历史缴费</button>
						<button class="cu-btn sm bg-green margin-left" @click="payFee(item)">缴费</button>
					</view>
				</view>
			</view>
			<view v-else class="noData">
				暂无数据
			</view>
		</view>
	</view>
</template>

<script>
	// pages/payParkingFeeList/payParkingFeeList.js
	const context = require("../../context/Java110Context.js");
	const constant = context.constant;
	const util = context.util;

	export default {
		data() {
			return {
				parkingSpaces: [],
				curParkingSpace: {},
				moreParkingSpaces: [],
				needFefresh: true,
				noData: false
			};
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let _that = this;
			if (!this.needFefresh) {
				this.needFefresh = true;
				return;
			}
			context.getOwner(function(_owner) {
				_that._loadParkingSpace(_owner);
			});
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			payFee: function(_item) {
				uni.navigateTo({
					url: '/pages/picker-view/payParkingFee?fee=' + JSON.stringify(_item),
				})
			},
			_loadParkingSpace: function(_owner) {
				let _that = this;
				_that.moreParkingSpaces = [];
				let _objData = {
					page: 1,
					row: 10,
					ownerId: _owner.memberId,
					communityId: _owner.communityId
				}
				context.request({
					url: constant.url.queryParkingSpacesByOwner,
					header: context.getHeaders(),
					method: "GET",
					data: _objData, //动态数据
					success: function(res) {
						console.log(res);
						if (res.statusCode == 200) {
							//成功情况下跳转
							let _parkingSpaces = res.data.parkingSpaces;
							_that.parkingSpaces = _parkingSpaces;
							if (_parkingSpaces.length == 0) {
								_that.noData = true;

								return;
							}
							_that.curParkingSpace = _parkingSpaces[0];

							_that._loadParkingSpaceFee();
						}
					},
					fail: function(e) {
						wx.showToast({
							title: "服务器异常了",
							icon: 'none',
							duration: 2000
						})
					}
				});
			},
			_loadParkingSpaceFee: function() {
				let _that = this;
				let _objData = {
					page: 1,
					row: 30,
					payerObjId: _that.curParkingSpace.psId,
					communityId: _that.curParkingSpace.communityId
				}

				_that.moreParkingSpaces = [];
				context.request({
					url: constant.url.queryFeeByOwner,
					header: context.getHeaders(),
					method: "GET",
					data: _objData, //动态数据
					success: function(res) {
						if (res.statusCode == 200) {
							//成功情况下跳转
							let _parkingSpaceFees = res.data.fees;
							if (_parkingSpaceFees.length < 1) {
								_that.noData = true;
							}
							_parkingSpaceFees.forEach(function(_fee) {
								let _tmpEndTime = _fee.endTime.replace(/\-/g, "/")
								let _endTime = new Date(_tmpEndTime);

								_fee.endTime = util.date.formatDate(_endTime);
								_fee.num = _that.curParkingSpace.num;
								let _now = new Date();

								if (_endTime > _now) {
									_fee.feeStateName = '正常'
								} else {
									_fee.feeStateName = '欠费'
								}
								_that.moreParkingSpaces.push(_fee);
							});

						}
					},
					fail: function(e) {
						wx.showToast({
							title: "服务器异常了",
							icon: 'none',
							duration: 2000
						})
					}
				});
			},
			payFeeDetail: function(_item) {
				uni.navigateTo({
					url: '/pages/payFeeDetail/payFeeDetail?fee=' + JSON.stringify(_item),
				});
			},
			switchParkingSpace: function(_parkingSpace) {
				this.curParkingSpace = _parkingSpace;
				this.noData = false;
				this._loadParkingSpaceFee();
			}
		}
	};
</script>
<style scoped>
	@import "../../style/main.css";
	.ppfl_footer{
	  text-align: right;
	}
	.ppfl_footer .ppfl_footer_his{
	  margin-right: 20rpx;
	}
	.bg-green{
		background-color: #e41f19;
	}
	.ppfl_c{
	  padding: 0rpx 20rpx 20rpx 20rpx;
	}
	
	.block__title {
	  margin: 0;
	  font-weight: 400;
	  font-size: 14px;
	  color: rgba(69,90,100,.6);
	  padding: 40rpx 30rpx 20rpx;
	}
	
	.ppfl_context{
	  padding: 20rpx 40rpx 40rpx 40rpx;
	  font-size: 28rpx;
	  color: #8a8a8a;
	}
	
	.ppfl_context .ppfl_context_row{
	  margin-top: 30rpx;
	}
	
	
	.button_up_blank{
	  height: 40rpx;
	}

	.solid-bottom::after {
		border-bottom: 2upx solid rgba(0, 0, 0, 0.1);
	}

	.solid-top::after {
		border-top: 2upx solid rgba(0, 0, 0, 0.1);
	}

	.margin-header-top {
		height: 100upx;
	}

	/*  #ifdef  APP-PLUS || MP-WEIXIN  */
	.header_fixed {
		position: fixed;
		top: 0upx;
		left: 0;
		width: 100%;
		z-index: 2;
	}
	/*  #endif  */
	/*  #ifdef  H5  */
	.header_fixed {
		position: fixed;
		top: 80upx;
		left: 0;
		width: 100%;
		z-index: 2;
	}
	/*  #endif  */
</style>
